import * as module from "@minecraft/server"
import * as util from "./utilities.js"

module.world.events.blockPlace.subscribe( data => util.init( data.block, data.dimension ) )

module.world.events.blockBreak.subscribe( data => util.init( data.block, data.dimension ) )

module.world.events.itemStopUseOn.subscribe(e => {

  const BLOCK = e.source.getBlockFromViewVector()
  const blockName = BLOCK.typeId.split(':')[1]

  if ( /double_\w+_slab/.test(blockName) ) util.init( BLOCK, e.source.dimension )
})

/* module.world.events.itemUseOn.subscribe(e => {
  if (e.item.id !== 'minecraft:wooden_sword') return

  const BLOCK = e.source.dimension.getBlock(e.blockLocation)
  const PERMUT = BLOCK.permutation

  e.source.dimension.runCommand(`say data:a = ${PERMUT.getProperty('data:a').value}`)
  e.source.dimension.runCommand(`say data:b = ${PERMUT.getProperty('data:b').value}`)
  e.source.dimension.runCommand(`say data:c = ${PERMUT.getProperty('data:c').value}`)
})

module.world.events.chat.subscribe(e => {
  const BLOCK = e.sender.getBlockFromViewVector()
  const msg = e.message.split(' ')

  if (BLOCK.id.split(':')[0] !== 'glass_pane') return
  if (msg[0] !== 'set') return

  console.warn('masok')

  let permut = BLOCK.type.createDefaultBlockPermutation()
  permut.getProperty('data:a').value = Number.parseInt(msg[1])
  permut.getProperty('data:b').value = Number.parseInt(msg[2])
  permut.getProperty('data:c').value = Number.parseInt(msg[3])

  BLOCK.setPermutation(permut)

}) */